﻿package _Pan3D.model{
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.display.BitmapData
	import flash.display.Bitmap
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat
	import flash.events.IOErrorEvent;
	import flash.events.Event
	import flash.events.*
    
	
	import flash.display.GraphicsTrianglePath;
    import flash.display.IGraphicsPath;
    import flash.display.IGraphicsData;
    import flash.display.GraphicsBitmapFill;
    import flash.display.TriangleCulling;
    import flash.display.GraphicsStroke;
    import flash.display.GraphicsSolidFill;
	import flash.display.Graphics
	import flash.geom.Vector3D;
	
	import _Pan3D.cameras.Camera3D;
	import _Pan3D.base.Object3D
	import _Pan3D.base.stat_3D
	import _Pan3D.model.M3D_Model

	

	
    // --------------MSN:lation_pan@live.cn  QQ: 3423526------------- //

    public class M3D_Model_stat3D extends M3D_Model {
		
		private var _Bmpsource:BitmapData //原始图像数据
		private var _triangles:GraphicsTrianglePath;        //存取三角形数据
		private var _base_vitem:Array = new Array();
		private var _vitem:Array = new Array();

		private var _v_array:Array = new Array();
		private var _u_array:Array = new Array();
		private var _uv_array:Array = new Array();
		
		private var _stringV:String
		private var _stringU:String
        private var _stringUV:String
		


		private var _load_pic:Loader = new Loader();
	
		public function M3D_Model_stat3D() {
			
		}
		public function _init(_tcam,_obj) {
			
			_Cam = _tcam;
			_this3d = _obj
			_this3d._loadOK=false
			
			this.x=_Cam._fovw/2
			this.y=_Cam._fovh/2
			
			
			var send_url = String("_File/stat3D_mode/" + String(_this3d._et) + ".xml");
		
			var temp_data:URLVariables = new URLVariables();
			var get_function=back_data;
			get_xml(send_url, temp_data, get_function);
			
		}
		public function _set_new_position(_obj:Object) {
			_this3d.x = _obj.x;
			_this3d.y = _obj.y;
			_this3d.z = _obj.z;
			choose_angel_y(_base_vitem, _vitem, _this3d.angle_y)
		}
		public function _change_angle(_num:Number) {
			_this3d.angle_y=_this3d.angle_y+_num
			choose_angel_y(_base_vitem, _vitem, _this3d.angle_y)
		}
		private function back_data(_xml:XML) {
		
			_stringV=String(_xml.v)
			_stringU = String(_xml.u)
			_stringUV = String(_xml.uv)
			var scale_D=Number(_xml.s)
			var i=0
			var temp_array:Array = new Array();
			
			temp_array = (_stringV.split(" "))   //拆分 3D点
			for (i = 0; i < temp_array.length / 3; i++ ) {
				_v_array.push(new Vector3D(temp_array[i * 3 + 0] * scale_D, temp_array[i * 3 + 2] * scale_D, temp_array[i * 3 + 1] * scale_D))
			}
			temp_array = (_stringU.split(" "))  //拆分 2D 贴图
			for (i = 0; i < temp_array.length / 3; i++ ) {
				_u_array.push(new Array(temp_array[i * 3 + 0], 1-temp_array[i * 3 + 1],temp_array[i * 3 + 2]));
			}
			
			temp_array = (_stringUV.split(" "))   //折分UV 对应坐标
			for (i = 0; i < temp_array.length/2 ; i++ ) {
				_uv_array.push(new Array(temp_array[i*2+0],temp_array[i*2+1]))
			}
			_triangles = new GraphicsTrianglePath(new Vector.<Number>(), new Vector.<int>(), new Vector.<Number>(),TriangleCulling.POSITIVE );

			for (i = 0; i < _v_array.length ;i++ ) {
				var _obj:Object3D = new Object3D
				    _obj.x = _v_array[i].x
				    _obj.y = _v_array[i].y
				    _obj.z = _v_array[i].z
					_obj.rx = 0;
					_obj.ry = 0;
					_obj.rz = 0;
				var _obj1:Object3D = new Object3D
				    _obj1.x = _v_array[i].x
				    _obj1.y = _v_array[i].y
				    _obj1.z = _v_array[i].z
					_obj1.rx = 0;
					_obj1.ry = 0;
					_obj1.rz = 0;
				_base_vitem.push(_obj);
				_vitem.push(_obj1);
			}
		
			
		    choose_angel_y(_base_vitem,_vitem,_this3d.angle_y)
			
			for (i = 0; i < _uv_array.length; i++ ) { 
				_triangles.vertices.push(0, 0);
				_triangles.uvtData.push(_u_array[_uv_array[i][1]][0], _u_array[_uv_array[i][1]][1], 1);
			}
			
		    _load_pic = new Loader();
			var request:URLRequest = new URLRequest(String("_File/stat3D_mesh/"+String(_this3d._et)+".jpg"))
            _load_pic.load(request);
			_load_pic.contentLoaderInfo.addEventListener(Event.COMPLETE, _load_pic_ok);
			
		}
		private function _load_pic_ok(evt:Event) {
			var loader:Loader = Loader(evt.target.loader);
            var image:Bitmap = Bitmap(loader.content);
            _Bmpsource= image.bitmapData.clone()
			_this3d._loadOK = true
			this.addEventListener(MouseEvent.MOUSE_DOWN, _on_Clik_Down)
			//this.useHandCursor = true
			//this.mouseEnabled = true
			//this.buttonMode=true
		}
		private function _on_Clik_Down(evt:MouseEvent) {
			//Object(root)._clik_obj(_this3d)
		}
		
		private function get_xml(send_url,send_data,get_function) {
			var request:URLRequest=new URLRequest(send_url);
			request.data=send_data;
			request.method=URLRequestMethod.POST;
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			function completeHandler(evt:Event) {
				get_function(XML(loader.data));
			}
			function ioErrorHandler(event:IOErrorEvent):void {

				get_function(XML("<td>error</td>"));
			}
			loader.load(request);
			
		}
		public function choose_angel_y(_vitem1, _vitem2, _angle) {
			var rx,ry,rz;
			for (var i in _vitem1) {
				rx = _vitem1[i].x;
				ry = _vitem1[i].y;
				rz = _vitem1[i].z;
				var tmp_angle = _angle*3.1415926/180;
				var tmp_rx = rx;
				rx = (Math.cos(tmp_angle) * tmp_rx - Math.sin(tmp_angle) * rz);
				rz = (Math.sin(tmp_angle) * tmp_rx + Math.cos(tmp_angle) * rz);
				_vitem2[i].x = rx + _this3d.x;
				_vitem2[i].y = ry + _this3d.y;
				_vitem2[i].z = rz + _this3d.z;
				
			}
		}
		public function _up_data() {

			
			if(_this3d._loadOK ){
				math_change_point(_this3d)
				_triangles.indices = new Vector.<int>();
				_addvertices=new Vector.<Number>()
				_adduvtData = new Vector.<Number>()
				_addind = new Vector.<int>();

				for (var i in _vitem) { 
					math_change_point(_vitem[i])
				}
				
				get_disp_rec()
				_draw_pic()
				
			
				if (_addvertices.length >= 1) {
						
					this.graphics.beginBitmapFill(_Bmpsource);
					this.graphics.drawTriangles(_addvertices, _addind, _adduvtData, TriangleCulling.POSITIVE );
				}else {
			
				}
			}
		}
		
		  private function get_disp_rec() {
			var _A0, _A1, _A2;
			var z_array:Array=new Array()
		
			for (var j = 0; j < _uv_array.length/3 ; j++ ) {
				_A0 = _vitem[(_uv_array[j * 3 + 0 ][0])];
				_A1 = _vitem[(_uv_array[j * 3 + 1 ][0])];
				_A2 = _vitem[(_uv_array[j * 3 + 2 ][0])];

				if (_A0.rz <  -_Cam._br && _A1.rz <  -_Cam._br  && _A2.rz < -_Cam._br ) {
				
				}else {
					if (_A0.rz <  -_Cam._br || _A1.rz <  -_Cam._br  || _A2.rz < -_Cam._br ) {
						get_suit_rec(_A0,_A1,_A2,{ x:_triangles.uvtData[j * 9 + 0], y:_triangles.uvtData[j * 9 + 1] }, { x:_triangles.uvtData[j * 9 + 3], y:_triangles.uvtData[j * 9 + 4] }, { x:_triangles.uvtData[j * 9 + 6], y:_triangles.uvtData[j * 9 + 7] } )
					
					}else {
						
							
							_triangles.vertices[j * 6 + 0] = _A0.dx;
							_triangles.vertices[j * 6 + 1] = _A0.dy;
							_triangles.vertices[j * 6 + 2] = _A1.dx;
							_triangles.vertices[j * 6 + 3] = _A1.dy;
							_triangles.vertices[j * 6 + 4] = _A2.dx;
							_triangles.vertices[j * 6 + 5] = _A2.dy;
							//_triangles.indices.push(j * 3 + 0, j * 3 + 1, j * 3 + 2);
							_triangles.uvtData[j * 9 + 2] = _A0.dz;
							_triangles.uvtData[j * 9 + 5] = _A1.dz;
							_triangles.uvtData[j * 9 + 8] = _A2.dz;
							var _tempZ = _A0.rz + _A1.rz + _A2.rz
							z_array.push({id:j,rz:_tempZ})

						
					}
				}
			}
			
			z_array.sortOn(["rz"], [Array.DESCENDING | Array.NUMERIC]);
			for (var k in z_array) {
				var _num = z_array[k].id
				_triangles.indices.push(_num*3+0,_num*3+1,_num*3+2)
			}
			
		}


		public function _draw_pic() {
			this.graphics.clear()
			if (_triangles.indices.length > 0) {
				//this.graphics.lineStyle(1,546245)
				this.graphics.beginBitmapFill(_Bmpsource);    //贴图文件，BitmapData
				this.graphics.drawTriangles(_triangles.vertices, _triangles.indices, _triangles.uvtData, TriangleCulling.POSITIVE); // 画图函数
			}else {
			
			}
		}

	}
	
}